﻿using ITXHWEB.Models;
using ReflectionStudio.Core.Events;
using System;
using System.Data;
using System.Web.Mvc;
using WPFExcelReport;
using WPFExcelReport.Helpers;
using ITXHWEB.Infrastructure;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
namespace ITXHWEB.Controllers
{
    public partial class WBBSTController
    {
        [ChildActionOnly]
        public ActionResult SSTKNR(string returnUrl)
        {
            ModelState.Clear();
            ViewBag.IsVAFReady = MABF.IsVAFReady;
            ViewBag.IsSTKNRReady = MABF.IsSTKNRReady;
            ViewBag.IsEXORDReady = MABF.IsEXORDReady; 
            ViewBag.ReturnUrl = returnUrl;
            if (!MABF.IsSTKNRReady)
            {
                INFO = "请扫描有效的单号";
            }
            return PartialView(MABF.VWBB);
        }
        [HttpPost]
        [AllowAnonymous]
        [ValidateAntiForgeryToken]
        public ActionResult SSTKNR(WBBSTModel model, string returnUrl)
        {
            try
            {
                ModelState.Clear();
                if (model.WBBSTKNR.Trim().Length < 3)
                {
                    return View();
                }
                MABF.STKNR = model.WBBSTKNR;
                CHKITX(model); 
            }
            catch (Exception ex)
            {
                INFO = ex.Message;
                Tracer.Error(ex.StackTrace, ex);
            }
            return RedirectToAction(MAPP, MAPP);
        }
        public void CHKITX(WBBSTModel model)
        {
            PM["STKNR"] = MABF.STKNR;
            PM["ART"] = MABF.ART;
            PM["ABAUF"] = MABF.ABPAUF;
            SResult = VHP.WBB.QITXWBB(PM);
            foreach (DataRow row in SResult.Tables[0].Rows)
            {
                MABF.IsVAFReady = true;
                MABF.IsEXORDReady = true;
                MABF.IsSTKNRReady = true;
                MABF.VWBB.IsERKReady = false;
                MABF.VWBB.WBBSTKNR = row["WBBSTKNR"].ToString();
                MABF.VWBB.ERVER = 0;  
            }
            INTEXVALIDATE();
        }
        public void INTEXVALIDATE()
        {
           if (MABF.IsSTKNRReady)
                return;
         
            PM["STKNR"] = MABF.STKNR;
            PM["ART"] = MABF.ART;
            PM["ABAUF"] = MABF.STKNR;
            SResult = QEXTSOURCE(PM);
            if (SResult.Tables[0].Rows.Count == 0)
                return;
           
            foreach (DataRow row in SResult.Tables[0].Rows)
            {
                Helper.GetSTime(ref date, ref time, ref stime);
                MABF.WFART = "F" +  Helper.Left(stime + row["WBBART"].ToString().PadRight(20), 20)
                       + Helper.Left(row["WBBVAR"].ToString().PadRight(2), 2)
                       + Helper.Left(row["WBBFARBE"].ToString().PadRight(10), 10)
                       + Helper.Left(row["WBBBESCHICH"].ToString().PadRight(10), 10)
                        + Helper.Left(row["WBBDESS"].ToString().PadRight(10), 10)
                       + Helper.Left(row["WBBCOLORIT"].ToString().PadRight(10), 10)
                          + "".PadRight(7)
                       + Helper.Left(row["WABEZ"].ToString().PadRight(20), 20);
                MABF.WAART = MABF.WFART.Substring(0, 23);
                MABF.EXORD = Helper.Left(row["PRODORDER"].ToString().PadRight(11), 11)
                   + MABF.WFART
                    + Helper.Left(row["KDKUNDNA"].ToString().PadRight(8), 8)
                    + Helper.Left(row["LABELTYP"].ToString().PadRight(20), 20);
                break;
            }
            MABF.IsVAFReady = false;
            MABF.IsEXORDReady = false;
        }
        private DataSet QEXTSOURCE(Dictionary<string, object> pm)
        {
            string _ABAUF = pm["ABAUF"].ToString();
            string _ART = pm["ART"].ToString();
            //string sql = " AND WBBART||WBBVAR||WBBFARBE LIKE '" + _ART.Trim() + "%'" + " AND WBBRWKRS||WBBRWLFD||WBBPOS LIKE '" + _ABAUF.Trim() + "%'";
            string sql = " AND WBBART||WBBVAR||WBBFARBE LIKE '" + _ART.Trim() + "%'";
            SResult = VHP.WBB.QWBEX91(sql);
            return SResult;
        }
    }
}
